package com.xianye.erp.plugin.log;

import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.xianye.erp.util.Logger;

@Aspect    
@Component 
public class SystemLogAspect {
	private static Logger log = Logger.getLogger(SystemLogAspect.class);
	
	 //Controller层切点    
    //@Pointcut("@annotation(com.fh.plugin.log.ControllerLog)") 
    @Pointcut("execution(* com.fh.controller.api.*.*(..))") 
    public  void controllerAspect() {    
    } 
    @Before("controllerAspect()")    
    public  void doBefore(JoinPoint joinPoint) {
    	HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); 
    	//请求的IP
    	StringBuilder sb = new StringBuilder();
        String ip = request.getRemoteAddr();
        String url = request.getRequestURI();
        log.info("IP地址："+ip);
        System.out.println("请求方法:" + (joinPoint.getTarget().getClass().getName() + "." + joinPoint.getSignature().getName() + "()"));  
        Enumeration<String> e = request.getParameterNames();
        while(e.hasMoreElements()){
        	String name = e.nextElement();
        	String[] values = request.getParameterValues(name);
        	if (values.length == 1) {
				sb.append(name).append("=").append(values[0]);
			}
        	else {
				sb.append(name).append("[]={");
				for (int i=0; i<values.length; i++) {
					if (i > 0)
						sb.append(",");
					sb.append(values[i]);
				}
				sb.append("}");
			}
        }
        log.error("请求链接"+url+sb.toString());
        System.out.println("请求参数");
        System.out.println("请求结果");
    }
}
